<%--
  访问地址： http://localhost:8099/myJspDemo/main.jsp?micro_route=hello
  访问地址： http://localhost:8099/myJspDemo/main.jsp?micro_route=user
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String appUrl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
  <title>子应用JSP-微前端模式-模拟路由跳转</title>
  <link href="<%= appUrl %>/css/style.css" rel="stylesheet">
<%--  <base href="${pageContext.request.contextPath}/">--%>
  <base href="<%= appUrl %>/">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <script>
  // 防止子应用修改history
  if(window.__MICRO_APP_ENVIRONMENT__) {
    console.log('====myJspDemo history')
    window.history.replaceState = () => {}
  }
  </script>

  <%
  String microRoute = request.getParameter("micro_route");
  if(microRoute == null) microRoute = "index.jsp";
  System.out.println("micro_route："+microRoute);

  switch(microRoute) {
    case "hello":
      //request.getRequestDispatcher("/hello-servlet").include(request, response);
      microRoute = "/hello-servlet";
      break;
    case "user":
      //request.getRequestDispatcher("/user-servlet").include(request, response);
      microRoute = "/user-servlet";
      break;
    default:
      // 显示默认内容
      //out.println("<h1>Welcome to JSP App</h1>");
      //out.println("<p>Current route: " + microRoute + "</p>");
      microRoute = "index.jsp";
      break;
  }
  %>
  <jsp:include page="<%= microRoute %>"/>
</body>
</html>